package com.dora.common.valid;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.HashSet;
import java.util.Set;

public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private Set<Integer> set=new HashSet<>();

    //初始化方法
    @Override
    public void initialize(ListValue constraintAnnotation) {

        //获取到注解中的值,存在Set<Integer> set，方便判断是否校验成功
        int[] vals1 = constraintAnnotation.vals();
        for (int val : vals1) {
            set.add(val);
        }


//        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    //判断是否校验成功
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        //判断是否在Set的值中
        return set.contains(integer);
    }
}
